package com.fang.study.util;

import com.fang.study.watch.DefaultWatch;
import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.CountDownLatch;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/7/6 4:22 下午
 **/
public class ZkUtils {
    private static ZooKeeper zk;
    /**
     * /path 代表当前操作访问的根目录
     */
    private static final String address = "101.200.208.140:2181/testLock";
    private static final DefaultWatch watch = new DefaultWatch();
    private static final CountDownLatch cdl = new CountDownLatch(1);

    public static ZooKeeper getZk() {
        try {
            watch.setCountDownLatch(cdl);
            zk = new ZooKeeper(address, 1000, watch);
            cdl.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zk;
    }
}
